package com.bjb.vr.common.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjb.vr.common.ao.OptCusResBundleAo;
import com.bjb.vr.common.constant.ThreadLocalConstant;
import com.bjb.vr.common.dto.ResBundle;
import com.bjb.vr.common.mapper.ResBundleMapper;
import com.bjb.vr.common.mapper.ResContentMapper;
import com.bjb.vr.common.utils.ThreadLocalUtil;
import com.bjb.vr.common.vo.OptCusResBundleContentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @version 1.0.0
 * @description:
 * @author: caihaibo
 * @createDate: 2022/9/16
 */
@Service
public class OptCusResBundleService extends ServiceImpl<ResBundleMapper, ResBundle> {

	@Autowired
	private ResBundleMapper resBundleMapper;

	@Autowired
	private ResContentMapper resContentMapper;

	/**
	 * 客户配置管理-资源包管理-查询资源包分页列表
	 * @param optCusResBundleAo
	 * @return
	 */
	public IPage<ResBundle> queryCusResBundleList(OptCusResBundleAo optCusResBundleAo){
		Assert.notNull(optCusResBundleAo.getTenantId(), "租户id不能为空！");
		ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);

		Page<ResBundle> resBundlePage = new Page<>(optCusResBundleAo.getCurrent(),optCusResBundleAo.getSize());
		LambdaQueryWrapper<ResBundle> queryWrapper = Wrappers.lambdaQuery(ResBundle.class);
		//查询条件
		queryWrapper.eq(ResBundle::getTenantId,optCusResBundleAo.getTenantId());
		//执行查询
		Page<ResBundle>  resBundlePage1 = this.page(resBundlePage,queryWrapper);
		IPage<ResBundle> iPage = new Page<>(resBundlePage1.getCurrent(),resBundlePage1.getSize(),resBundlePage1.getTotal());
		iPage.setRecords(resBundlePage1.getRecords());

		ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, false);

		return iPage;
	}

	/**
	 * 客户配置管理-资源包管理-查看包内资源列表
	 * @param optCusResBundleAo
	 * @return
	 */
	public List<OptCusResBundleContentVo> queryCusResBundleDetailList(OptCusResBundleAo optCusResBundleAo){
		Assert.notNull(optCusResBundleAo.getBundleId(), "资源包id不能为空！");

		ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);

		List<OptCusResBundleContentVo> optCusResBundleContentVoList = resContentMapper.queryCusResBundleDetailList(optCusResBundleAo.getBundleId());

		ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, false);
		return optCusResBundleContentVoList;
	}
}
